<?php
require_once ('config.php');
require_once ('databasefunctions.php');
require_once ('displayfunctions.php');
validate_user();
html_header();

//Show menu

switch ($_GET['action']) {
        case "add_form" :
                title_bar($langcompanymaster);
                ?>
                <br><? echo $langoperation . " - " . $langadd ?><br>
                <form method = "GET" action="companymaster.php">
                <table>
                        <tr>
                                <td bgcolor='<? echo $color1 ?>'><? echo $langcompanyid ?>
                                <td><input type = 'text' name = 'CompanyID' maxlength = '10'>
                        <tr>
                                <td bgcolor='<? echo $color1 ?>'><? echo $langcompanyname ?>
                                <td><input type = 'text' name = 'CompanyName' maxlength = '50'>
                        <tr>
                                <td bgcolor='<? echo $color1 ?>'>Extra
                                <td><input type = 'text' name = 'additional' maxlength = '150'>
                        <tr>
                                <td bgcolor='<? echo $color1 ?>'><? echo $langenablestatus ?>
                                <td><select name = 'Enable'>
                                                <option value='0'><? echo $langdisable ?>
                                                <option value='1'><? echo $langenable ?>
                                        </select>
                </table>
                <input type = 'hidden' name = 'action' value = 'add'>
                <input type = 'submit' value = '<? echo $langadd ?>'>
                </form>
                <?
                break;

        case "add" :
                if (empty($_GET['CompanyID'])) {
                        message_box($langcompanyid . " " . $langcannotempty);
                        ?>
                                <script>history.go(-1);</script>
                        <?
                        exit;
                }
                $sql = "INSERT INTO companymaster (CompanyID, CompanyName, additional, Enable) values (";
                $sql .= "'" . strtoupper($_GET['CompanyID']) . "', ";
                $sql .= "'" . $_GET['CompanyName'] . "', ";
                $sql .= "'" . $_GET['additional'] . "', ";
                $sql .= "'" . $_GET['Enable'] . "'";
                $sql .= ")";

                $sql_result = sql_execute($sql);
                echo "<br>$langcompanyid $langadded<br>";
                title_bar($langcompanymaster, 1);
                break;

        case "modify_form" :

                title_bar($langcompanymaster, 1);
                // Obtain values from table
                $sql = "SELECT * FROM companymaster WHERE CompanyID='" . $_GET['CompanyID']. "'";
                $sql_result = sql_execute($sql);
                $sql_data = mysql_fetch_array($sql_result);

                ?>
                <br><? echo $langoperation . " - " . $langmodify ?><br>
                <form method = "GET" action="companymaster.php">
                <table>
                        <tr>
                                <td bgcolor='<? echo $color1 ?>'><? echo $langcompanyid ?>
                                <td><input type = 'text' name = 'CompanyID' value = '<? echo $_GET['CompanyID'] ?>'
                                        maxlength = '10' readonly>
                        <tr>
                                <td bgcolor='<? echo $color1 ?>'><? echo $langcompanyname ?>
                                <td><input type = 'text' name = 'CompanyName' maxlength = '50' value='<?
                                        echo $sql_data['CompanyName'] ?> '>
                         <tr>
                                <td bgcolor='<? echo $color1 ?>'>Extra
                                <td><input type = 'text' name = 'additional' maxlength = '255' value='<?
                                        echo $sql_data['additional'] ?> '>
                        <tr>
                                <td bgcolor='<? echo $color1 ?>'><? echo $langenablestatus ?>
                                <td><select name = 'Enable'>
                                        <?
                                        if ($sql_data['Enable'] == '0') {
                                                echo "<option value='0' SELECTED>$langdisable";
                                                echo "<option value='1'>$langenable";
                                        }
                                        else {
                                                echo "<option value='0'>$langdisable";
                                                echo "<option value='1' SELECTED>$langenable";
                                        }
                                        ?>
                                        </select>
                </table>
                <input type = 'hidden' name = 'action' value = 'modify'>
                <input type = 'submit' value = '<? echo $langmodify ?>'>
                </form>
                <?
                break;

        case "modify" :
                $sql = "UPDATE companymaster SET ";
                $sql .= "CompanyName = '" . $_GET['CompanyName'] . "', ";
                $sql .= "additional = '" . $_GET['additional'] . "', ";
                $sql .= "Enable = '" . $_GET['Enable'] . "' ";
                $sql .= "WHERE CompanyID = '" . $_GET['CompanyID'] . "'";

                $sql_result = sql_execute($sql);
                echo "<br>$langcompanyid $langmodified<br>";
                title_bar($langcompanymaster, 1);
                break;

        case "delete" :
                $sql = "SELECT CompanyID FROM billtransaction WHERE CompanyID = '" . $_GET['CompanyID'] ."' LIMIT 1";
                $sql_result = sql_execute($sql);
                $sql_data = mysql_fetch_array($sql_result);
                if ($sql_data[0]) {
                        $message = $langtransactionexist . "<br>" . $langcannotdelcompany . "<br>";
                        message_box($message);
                        ?>
                                <script>history.go(-1);</script>
                        <?
                        exit;
                }

                $sql = "DELETE FROM companymaster WHERE CompanyID = '" . $_GET['CompanyID'] . "'";

                $sql_result = sql_execute($sql);
                echo "<br>$langcompanyid $langdeleted<br>";
                title_bar($langcompanymaster, 1);
                break;

        case "count" :
                save_search_list();
                $url_text = retrieve_search_list();
                $sql = select_statement('companymaster');
                $sql_result = sql_execute($sql);
                $sql_data = mysql_fetch_array($sql_result);
                $no_of_record = $sql_data[0];

                ?>
                <script>
                this.location = 'companymaster.php?action=browse&total_record=<? echo $no_of_record ?>
                                                &record_offset=0<? echo $url_text ?>'
                </script>
                <?
                break;

        case "browse" :
                navigation_bar($langcompanymaster, 1);
                column_headings(array($langcompanyid, $langcompanyname, "Extra", $langenablestatus));
                // List companys here
                $sql = select_statement('companymaster',1);
                $sql.= " ORDER BY CompanyID LIMIT " . $_GET['record_offset'] . ", " . $_SESSION['RecordPerPage'];
                $sql_result = sql_execute($sql);

                while ($sql_data = mysql_fetch_array($sql_result)) {
                        $rowcolor = row_color_change();
                        echo "\n<tr bgcolor='$rowcolor'>";
                        echo "<td>";
                        echo "<a href = 'companymaster.php?action=modify_form&CompanyID=".$sql_data['CompanyID'];
                        echo "'>".$sql_data['CompanyID']."</a>";
                        echo "<td>";
                        echo $sql_data['CompanyName'];
                        echo "<td>";
                        echo $sql_data['additional'];
                        echo "<td>";
                        if ($sql_data['Enable'] == 1) echo $langyes;
                        else echo $langno;
                        echo "<td>";
                        echo "<a href = 'companymaster.php?action=delete&CompanyID=" .
                                        $sql_data['CompanyID'] ."'>$langdelete</a>";
                }
                ?>
                </table>
                <?
                break;

        default :
                title_bar($langcompanymaster, 1);
                $field[] = array('title'=>$langcompanyid, 'field'=>'CompanyID', 'length'=>10);
                search_form($field, $_SERVER['PHP_SELF']);
                break;
}

html_footer();
?>
